<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>企业列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="css/layui.css" rel="stylesheet"  media="all">
</head>
<body>

<table id="company-list"></table>

<script type="text/html" id="toolbarDemo">
  <div class="layui-inline">
    <input class="layui-input" name="keywords" id="keywords" placeholder="请输入企业名称" autocomplete="off" lay-affix="clear">
  </div>
  <button class="layui-btn" lay-event="search">搜索</button>
  <button class="layui-btn layui-btn-sm" lay-event="btnExport">全部导出</button>
</script>
<script type="text/html" id="authStatusTpl">
    {{#  if(d.authStatus ==0 ){ }}
        <p style="color: #bbaa0f">未认证</p>
    {{#  } else if(d.authStatus ==1 ){ }}
        <p style="color: #117dd4">认证通过</p>
    {{#  } else { }}
        <p style="color: #e40e0e">认证失败</p>
    {{# } }}
</script>
<script type="text/html" id="barDemo">
    {{#  if(d.authStatus !=1 ){ }}
    <a class="layui-btn layui-btn-xs" lay-event="auth">重新认证</a>
    
    {{#  } else { }}
        <p></p>
    {{# } }}
    
</script>  

<script src="js/layui.js"></script>
<script type="module">
    import company from './js/company.js'
    
    layui.use(['table', 'layer', 'form'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;

        // 加载企业列表
        var loadCompanyList = function(keywords,isExport){
            company.getCompanyList($,keywords,function(rspData){
            //表格数据渲染
            var insTable = table.render({
                elem: '#company-list'
                ,title:'企业列表'
                ,toolbar:'#toolbarDemo'
                ,width: 1360
                ,height: 600
                ,size: 'lg'
                ,cols: [[{ type:"numbers", width:80, title: '序号', sort: true, fixed: 'left'}
                ,{field:'id', hide:true}
                ,{field:'name', width:200, title: '企业名称'}
                ,{field:'trade', width:220, title: '所属行业',sort:true }
                ,{field:'region', width:180, title: '所属区域',sort:true }
                ,{field:'authStatus', width:150, title: '认证状态', align: 'center', sort:true, templet: '#authStatusTpl'}
                ,{field:'userName', width:120, title: '创建人'}
                ,{field:'phoneNumber', width:120, title: '电话'}
                ,{field:'createTime', width:160, title: '创建时间'}
                ,{fixed: 'right', title:'操作', width: 100, toolbar: '#barDemo'}
                ]]
                ,data: rspData.data
                ,done:function(){
                    $('#keywords').val(keywords);
                }
                ,even: true
                ,page: true //是否显示分页
                ,limits: [20,50,100]
                ,limit: 20 //每页默认显示的数量
                });
                if(isExport){
                    table.exportFile(insTable.config.id,rspData.data,'xls');
                }
            })
        }
        loadCompanyList(null);

        // 工具栏事件
        table.on('toolbar(company-list)', function(obj){
            switch(obj.event){
                case 'search':
                    var keywords = $('#keywords').val();
                    loadCompanyList(keywords);
                break;
                case 'btnExport':
                    var keywords = $('#keywords').val();
                    loadCompanyList(keywords,true);
                break;
            };
        });
        //触发单元格工具事件
        table.on('tool(company-list)', function(obj){
            if(obj.event == 'auth'){
                layer.confirm('您确认重新认证吗', function(index){
                    company.authCompany($,obj.data.id,function(rspData){
                        if(rspData.code ==1){
                            // var update = {};
                            // update['authStatus'] = 1;
                            // obj.update(update);
                            // 刷新列表
                            var keywords = $('#keywords').val();
                            loadCompanyList(keywords);
                            layer.msg("认证成功");
                        } else {
                            layer.msg("认证失败",{icon:5});
                        }
                    })
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>

</html>